package cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.entity.YwhPersonInfo;
import cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.mapper.YwhPersonInfoMapper;
import cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.model.params.YwhPersonInfoParam;
import cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.model.result.YwhPersonInfoResult;
import cn.stylefeng.guns.sys.modular.ownersCommitteeInfo.service.YwhPersonInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业委会人员表 服务实现类
 * </p>
 *
 * @author WangZhen
 * @since 2021-01-21
 */
@Service
public class YwhPersonInfoServiceImpl extends ServiceImpl<YwhPersonInfoMapper, YwhPersonInfo> implements YwhPersonInfoService {

    @Override
    public void add(YwhPersonInfoParam param) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        param.setIsDel(0);
        param.setCreatePerson(user.getName());
        param.setCreateDate(new Date());
        this.baseMapper.insertOwnersCommittee(param);
    }

    @Override
    public void delete(YwhPersonInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(YwhPersonInfoParam param) {
        YwhPersonInfo oldEntity = getOldEntity(param);
        YwhPersonInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public YwhPersonInfoResult findBySpec(YwhPersonInfoParam param) {
        return null;
    }

    @Override
    public List<YwhPersonInfoResult> findListBySpec(YwhPersonInfoParam param) {
        param.setIsDel(0);
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(YwhPersonInfoParam param) {
        Page pageContext = getPageContext();
        param.setIsDel(0);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Integer selectDeleteInfo(Integer param) {
        return this.baseMapper.selectDelete(param);
    }

    @Override
    public void updateDeleteInfo(Integer param) {
        this.baseMapper.updateDelete(param);
    }

    @Override
    public void updateOwnersCommitteeInfo(YwhPersonInfoParam param) {
        this.baseMapper.updateOwnersCommittee(param);
    }

    private Serializable getKey(YwhPersonInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private YwhPersonInfo getOldEntity(YwhPersonInfoParam param) {
        return this.getById(getKey(param));
    }

    private YwhPersonInfo getEntity(YwhPersonInfoParam param) {
        YwhPersonInfo entity = new YwhPersonInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
